package com.example.myactspringboot.controller;

import com.example.myactspringboot.bean.SysMenu;
import com.example.myactspringboot.bean.SysRoleMenu;
import com.example.myactspringboot.server.SysMenuService;
import com.example.myactspringboot.util.R2;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @Create 2023-09-23 15:45
 * @Description
 */
@Controller
@RequestMapping("/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService menuService;

    @GetMapping("/getMenus")
    @ResponseBody
    public R2 getMenusById(){
        Session session = SecurityUtils.getSubject().getSession();
        String username = (String) session.getAttribute("username");
        List<SysMenu> menus = menuService.getMenus(username);
        return R2.ok().data(menus);
    }
    @GetMapping("/getAllMenus")
    @ResponseBody
    public R2 getMenusAll(){
        List<SysMenu> menus = menuService.getMenus("");
        return R2.ok().data(menus);
    }
    @GetMapping("/toTreePage")
    public String toTreePage(){

        return "treeMenu";
    }

    @PostMapping("/saveMenu")
    @ResponseBody
    public R2 saveMenu(@RequestBody SysMenu menu){
        int i = menuService.saveMenu(menu);
        return R2.ok().data(i);
    }
    @PostMapping(value = "/updateMenu",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public R2 updateMenu(@RequestBody SysMenu menu){
//        System.out.println(menu);
        int i = menuService.updateMenu(menu);
        return R2.ok().data(1);
    }

    @GetMapping("/getMenusNoTree/{roleId}")
    @ResponseBody
    public R2 getMenusNoTree(@PathVariable String roleId){
        List<SysRoleMenu> menus = menuService.getMenusNoTree(roleId);
        return R2.ok().data(menus);
    }

    @PostMapping("/updateRoleMenu/{roleId}")
    @ResponseBody
    public R2 updateRoleMenu(@PathVariable String roleId, @RequestBody Map map){
        System.out.println(map);
        Integer integer = menuService.updateRoleMenu((List<String>) map.get("menuArr"), roleId);
        return integer >0?R2.ok().message("角色菜单更新成功"):R2.error().message("角色菜单更新失败");
    }
}
